#pragma once

#include "locallock.hpp"

int count6 = 100;

struct TreadDataCpp6
{
    std::string _name;
    My_local_lock::LocalLockCpp *_mymutex;
    TreadDataCpp6(std::string name, My_local_lock::LocalLockCpp *_mymutex)
        : _name(name), _mymutex(_mymutex)
    {
    }
};

void *thread6(void *argv)
{
    TreadDataCpp6 *mylock = static_cast<TreadDataCpp6 *>(argv);
    while (1)
    {
        mylock->_mymutex->Lock();
        if (count6 > 0)
        {
            usleep(1000);
            std::cout << "child thread tid: " << mylock->_name << " my count = " << count6 << std::endl;
            count6--;
            mylock->_mymutex->Unlock();
        }
        else
        {
            mylock->_mymutex->Unlock();
            return nullptr;
        }
    }
    return nullptr;
}

// cpp加锁
void cpp_mutex_struct2()
{
    std::string myname[5] = {"string1_name", "string2_name", "string3_name", "string4_name", "string5_name"};
    std::mutex mymutexlock;
    My_local_lock::LocalLockCpp mymutex(&mymutexlock);

    TreadDataCpp6 *td1 = new TreadDataCpp6(myname[0], &mymutex);
    pthread_t pth1_id;
    int pth1_ret = pthread_create(&pth1_id, NULL, thread6, td1);
    TreadDataCpp6 *td2 = new TreadDataCpp6(myname[1], &mymutex);
    pthread_t pth2_id;
    int pth2_ret = pthread_create(&pth2_id, NULL, thread6, td2);
    TreadDataCpp6 *td3 = new TreadDataCpp6(myname[2], &mymutex);
    pthread_t pth3_id;
    int pth3_ret = pthread_create(&pth3_id, NULL, thread6, td3);
    TreadDataCpp6 *td4 = new TreadDataCpp6(myname[3], &mymutex);
    pthread_t pth4_id;
    int pth4_ret = pthread_create(&pth4_id, NULL, thread6, td4);
    TreadDataCpp6 *td5 = new TreadDataCpp6(myname[4], &mymutex);
    pthread_t pth5_id;
    int pth5_ret = pthread_create(&pth5_id, NULL, thread6, td5);

    pthread_detach(pth1_id);
    pthread_detach(pth2_id);
    pthread_detach(pth3_id);
    pthread_detach(pth4_id);
    pthread_detach(pth5_id);

    while (count6 > 0)
    {
        usleep(1000);
    }
    sleep(2);
    delete td1;
    delete td2;
    delete td3;
    delete td4;
    delete td5;
}
